package com.mew.chess.core.game.yxx.game.message;

import com.mew.chess.core.game.yxx.game.common.YxxConstantValue;
import com.zebra.common.ZebraBaseMessage;
import io.netty.buffer.ByteBuf;
import lombok.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class YxxHistoryMessage extends ZebraBaseMessage<YxxHistoryMessage> {

    private List<int[]> history = new ArrayList<>(YxxConstantValue.HISTORY_NUMBER);; //negative lose; positive win

    //60把概率
    public int[] perType;


    public void addHistory(int[] h){

        if (null==history){
            history = new ArrayList<>(YxxConstantValue.HISTORY_NUMBER);
        }
        history.add(h);
        if (history.size()>YxxConstantValue.HISTORY_NUMBER){
            history.remove(0);
        }

        per();

    }

    public void per(){
        //最后60把概率
        int startId = history.size()-60;
        int[] typeNum = new int[6];
        history.stream().skip(startId>0?startId:0).forEach(h->{
            Arrays.stream(h).forEach(i->typeNum[i-1]++);
        });

        //算百分比
        int sum = Arrays.stream(typeNum).sum();
        for (int i = 0; i < typeNum.length; i++) {
            typeNum[i] = typeNum[i]*100/sum;
        }

        perType = typeNum;
    }


    @Override
    public void write(ByteBuf b) {

        writeMsg(b);
    }

    public void writeMsg(ByteBuf b) {
        int push = YxxConstantValue.HISTORY_NUMBER_PUSH;
        boolean m = history.size()>push;
        int size = m?push:history.size();
        int start  = m?history.size()-push:0;


        writeInt(b,size);
        for (int i = start; i < history.size(); i++) {
            writeIntArray(b,history.get(i));
        }

        writeIntArray(b,perType);

    }

    @Override
    public YxxHistoryMessage read(ByteBuf b) {
        return null;
    }
}
